package com.hyfrogx.modules.app.controller.course;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyfrogx.common.param.JSONResult;
import com.hyfrogx.common.utils.QueryExt;
import com.hyfrogx.common.utils.R;
import com.hyfrogx.modules.app.annotation.Login;
import com.hyfrogx.modules.app.entity.User;
import com.hyfrogx.modules.app.form.course.GetCourseSynopsisForm;
import com.hyfrogx.modules.manage.controller.BaseController;
import com.hyfrogx.modules.manage.entity.course.CourseManageEntity;
import com.hyfrogx.modules.manage.entity.course.CourseManageRemarkEntity;
import com.hyfrogx.modules.manage.entity.course.CourseManageSectionEntity;
import com.hyfrogx.modules.manage.entity.course.CourseOrderEntity;
import com.hyfrogx.modules.manage.form.course.CourseSectionSortForm;
import com.hyfrogx.modules.manage.service.course.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
/**
 * @author 罗春杰-技术部
 */
@RestController
@RequestMapping("api/course/free")
@Api(value = "ApiCourseFreeController",tags = {"客户端-课程无需登录模块"})
public class ApiCourseFreeController extends BaseController {

    @Autowired
    CourseManageService ManageService;

    @Autowired
    CourseManageRemarkService remarkService;

    @Autowired
    CourseManageSectionService sectionService;

    @Autowired
    CourseTypeService typeService;

    @Autowired
    CourseOrderService orderService;

    @Autowired
    CourseManageSectionWatchService watchService;


    /**
     * 获取上架课程列表
     *
     * @param
     * @return List<CourserManage>
     * @throws Exception
     */
    @RequestMapping(value = "/listCourseManage", method = RequestMethod.POST)
    @ApiOperation("获取上架课程列表")
    public R ListCourseManage(@RequestBody Map map) {
        try {
            // 本方法可能要重写，因为web端获取所有课程是包含未上线并且排序规则与客户端不同,分页估计也支持,如果条件不能满足。就加条件
            Page page = ManageService.listCourseManageOnline(new QueryExt(map));
            return R.ok().put("data", page);
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

    /**
     * 获取课程下评论列表
     *
     * @param
     * @return List<HyCourseManageRemark>
     * @throws Exception
     */
    @RequestMapping(value = "/listCourseRemark", method = RequestMethod.POST)
    @ApiOperation("获取课程下评论列表")
    public R ListCourseRemark(@RequestBody Map map) {
        try {
            Page page = remarkService.ListGetPageRemarkForStatus(new QueryExt(map));
            double sunGrade =remarkService.sunGrade(Convert.toInt(map.get("courseId")));
            double data = new BigDecimal(sunGrade).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
            if(sunGrade ==0){
                return R.ok().put("data",page).put("count",5);
            }else{
                return R.ok().put("data",page).put("count",data);
            }
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

    /**
     * 获取课程详情
     *
     * @param
     * @return HyCourseManage
     * @throws Exception
     */
    @RequestMapping(value = "/listCourseManageSynopsis", method = RequestMethod.POST)
    @ApiOperation("获取课程详情")
    public R ListCourseManageSynopsis(@RequestBody  GetCourseSynopsisForm form) {
        try {
            int newNumber = sectionService.count(new QueryWrapper<CourseManageSectionEntity>()
                    .eq("isputaway",1).eq("programid",form.getId()));
            CourseManageEntity result = ManageService.getById(form.getId());
            result.setNumbers(newNumber);
            if(form.getUid()!=null){
                int count = orderService.count(new QueryWrapper<CourseOrderEntity>()
                        .eq("uid",form.getUid()).eq("cid",form.getId()).eq("status",1));
                if (count > 0) {
                    result.setIsBuy(1);
                }
            }
            return R.ok().put("data",result);
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }


    /**
     * 查看上架的章节
     *
     * @param
     * @param
     * @param
     * @return
     */
    @RequestMapping(value = "/courseDetails", method = RequestMethod.POST)
    @ApiOperation("查看上架的章节")
    public R CourseDetails(@RequestBody Map map) {
        try {
            Page page = sectionService.getAllIsPutAwaySection(new QueryExt(map));
            return R.ok().put("data",page);
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

    /**
     * 增加课程点击数
     *
     * @param
     * @param
     * @param
     * @return
     */
    @RequestMapping(value = "/addPlaysall", method = RequestMethod.POST)
    @ApiOperation("增加课程点击数")
    public R addPlaysall(@RequestParam Integer id) {
        try {
            CourseManageEntity manageEntity = ManageService.getById(id);
            manageEntity.setPlaysall(manageEntity.getPlaysall()+1);
            ManageService.updateById(manageEntity);
            Integer playsallById = manageEntity.getPlaysall();
            return R.ok().put("data",playsallById);
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

    /**
     * 增加章节点击数
     *

     * @param
     * @return
     */
    @RequestMapping(value = "/addSectionPlays", method = RequestMethod.POST)
    @ApiOperation("增加章节点击数")
    public R addSectionPlays(@RequestParam Integer id) {
        try {
            CourseManageSectionEntity sectionEntity = sectionService.getById(id);
            sectionEntity.setPlays(sectionEntity.getPlays()+1);
            sectionService.updateById(sectionEntity);
            return R.ok("操作成功 ");
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

    /**
     * 增加分享数
     *
     * @return
     */
    @RequestMapping(value = "/addShares", method = RequestMethod.POST)
    @ApiOperation("增加分享数")
    public R addShares(@RequestParam Integer id) {
        try {
            CourseManageEntity manageEntity = ManageService.getById(id);
            manageEntity.setShares(manageEntity.getShares()+1);
            ManageService.updateById(manageEntity);
            return R.ok("操作成功 ");
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

    /**
     * 获取上架的课程分类
     *
     * @param
     * @param
     * @return
     */
    @RequestMapping(value = "/getAllcourseType", method = RequestMethod.POST)
    @ApiOperation("获取上架的课程分类")
    public R getAllcourseType(@RequestBody Map map) {
        try {
            Page page = typeService.ApiListGetByPage(new QueryExt(map));
            return R.ok().put("data",page);
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

    /**
     * 获取上/下章节
     *
     * @return
     */
    @RequestMapping(value = "/getTheSection", method = RequestMethod.POST)
    @ApiOperation("获取上/下章节")
    public R getTheSection(@Validated @RequestBody CourseSectionSortForm form) {

        try {
            CourseManageSectionEntity section = sectionService.getSectionBySort(form.getType(), form.getSort(), form.getProgramid());
            if (section == null) {
                return R.error("没有上/下章节");
            }

            return R.ok().put("data",section);
        } catch (Exception e) {
            return R.error(e.getMessage());
        }

    }
}
